﻿using System;
using System.Configuration;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using DynamicComparer;
using Shuba.Model;

namespace Shuba.Web.admin
{
    public partial class audit_trail_old : basePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                string acl = GetUserAccess("AUDIT_TRAIL");

                if (!IsUserView(acl))
                {
                    string msg = this.GetGlobalResourceObject("Messages", "NoPrivilege").ToString();
                    AlertAndRedirect(msg, ResolveUrl(ConfigurationManager.AppSettings["default_url"]));
                }

                LabelTitle.Text = GetPageTitle("AUDIT_TRAIL");

                Shuba.Business.AuditTrail log = new Shuba.Business.AuditTrail();
                List<AuditTrailInfo> TableList = log.GetTableList();
                ddlTable.DataSource = TableList;
                ddlTable.DataValueField = "TABLE_NAME";
                ddlTable.DataTextField = "TABLE_NAME";
                ddlTable.DataBind();

                txtStartDate.Text = ProcessingDate().ToString("dd MMMM yyyy");
                txtToDate.Text = ProcessingDate().ToString("dd MMMM yyyy");

                InitSort();
                BindData();
            }
        }

        protected void gvLog_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gvLog.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gvLog_OnSorting(object sender, GridViewSortEventArgs e)
        {
            SortDirection(e.SortExpression, e.SortDirection.ToString());
            ViewState["SortEx"] = e.SortExpression;
            gvLog.PageIndex = 0;
            BindData();
        }

        protected void gvLog_OnRowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                switch (e.Row.Cells[0].Text.ToUpper())
                {
                    case "I":
                        e.Row.Cells[0].Text = "INSERT";
                        break;
                    case "U":
                        e.Row.Cells[0].Text = "UPDATE";
                        break;
                    case "D":
                        e.Row.Cells[0].Text = "DELETE";
                        break;
                }
            }
        }
        
        private void BindData()
        {
            DateTime startDate, endDate;
            Shuba.Business.AuditTrail log = new Shuba.Business.AuditTrail();

            if (txtStartDate.Text != "") 
            {
                startDate = Convert.ToDateTime(txtStartDate.Text);
            }
            else
            {
                startDate = ProcessingDate();
            }

            if (txtToDate.Text != "")
            {
                endDate = Convert.ToDateTime(txtToDate.Text);
            }
            else
            {
                endDate = ProcessingDate();
            }

            try
            {
                List<AuditTrailInfo> list = log.GetList(ddlTable.SelectedValue.Trim(), startDate, endDate);
                if (ViewState["SortEx"].ToString().Trim() != "")
                {
                    string sort = sort = ViewState["SortEx"].ToString() + " " + ViewState["Sort"];
                    DynamicComparer<AuditTrailInfo> comparer = new DynamicComparer<AuditTrailInfo>(sort);
                    list.Sort(comparer);
                }

                gvLog.DataSource = list;
                gvLog.DataBind();
            }
            catch
            {
                //Alert(ex.Message);
            }

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //System.Threading.Thread.Sleep(3000);

            if (Convert.ToDateTime(txtToDate.Text) >= Convert.ToDateTime(txtStartDate.Text))
            {
                BindData();
            }
            else
            {
                string msg = this.GetGlobalResourceObject("Messages", "InvalidDateRange").ToString();
                Alert(msg);
            }

        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {
            gvLog.AllowPaging = false;
            BindData();
            GridViewExportUtil.Export("audit_" + ConfigurationManager.AppSettings["app.code"] + "_" + ProcessingDate().ToString("ddMMMyyyy") + ".xls", gvLog);

            //Response.Clear();
            //Response.AddHeader("content-disposition", "attachment;filename=log" + ProcessingDate().ToString("ddMMMyyyy") + ".xls");
            //Response.Charset = "";

            //// If you want the option to open the Excel file without saving than
            //// comment out the line below
            //// Response.Cache.SetCacheability(HttpCacheability.NoCache);

            //Response.ContentType = "application/vnd.xls";
            //System.IO.StringWriter stringWrite = new System.IO.StringWriter();
            //System.Web.UI.HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWrite);
            //gvLog.RenderControl(htmlWrite);

            //Response.Write(stringWrite.ToString());
            //Response.End();        
        }
    }
}
